home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_019 / blackjack / hndmgr.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  153 lines

  1. /*  HNDMGR.C      Hand manager
  2.  */
  3.  
  4. #include "local.h"
  5. #include "bj.h"
  6. #include "hndmgr.h"
  7. #include "dekmgr.h"
  8.  
  9. static char spots[13][3] =
  10.    {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
  11. static char suits[4][2] = {"S", "H", "D", "C"};
  12. static short hands[3][12] = {0,0,0};   /* three hands */
  13. static short ncards[3] = {0,0,0};      /* how many cards in each hand */
  14. static short tophand = 0;              /* how many player hands active */
  15.  
  16. /* allbst      - are all player's hands busted?
  17.  */
  18. bool allbst()
  19. {
  20.    short score();
  21.  
  22.    if (score(1) <= 21 || (tophand == 2 && score(2) <= 21))
  23.       return(NO);
  24.    else
  25.       return(YES);
  26. }
  27.  
  28. /* deal     - initialize the hands
  29.  */
  30. void deal()
  31. {
  32.    short tkcard();
  33.    void  show();
  34.  
  35.    hands[1][0] = tkcard();
  36.    hands[DEALER][0] = tkcard();
  37.    hands[1][1] = tkcard();
  38.    hands[DEALER][1] = tkcard();
  39.    ncards[DEALER] = ncards[1] = 2;
  40.    tophand = 1;
  41.    printf("The dealer shows ");
  42.    show(DEALER, 0);
  43.    printf("\nYou have ");
  44.    show(1,0);
  45.    printf(" + ");
  46.    show(1, 1);
  47.    printf("\n");
  48. }
  49.  
  50. /* hit  -  add a card to a hand
  51.  */
  52. bool hit(h)
  53.    short h;       /* which hand */
  54. {
  55.    short score(), tkcard();
  56.    void  show();
  57.  
  58.    hands[h][ncards[h]] = tkcard();
  59.    printf(" + ");
  60.    show(h, ncards[h]);
  61.    ++ncards[h];
  62.    if (21 < score(h) || h == DEALER && 17 <= score(h))
  63.       return(NO);
  64.    else
  65.       return(YES);
  66. }
  67.  
  68. /* ISBJ     - is hand a "natural" 2-card blackjack?
  69.  */
  70. bool isbj(h)
  71.    short h;       /* which hand */
  72. {
  73.    short score();
  74.  
  75.    if (h == DEALER)
  76.       return (ncards[DEALER] == 2 && score(DEALER) == 21);
  77.    else if (h == 1)
  78.       return (tophand == 1 && ncards[1] == 2 && score(1) == 21);
  79.    else
  80.       return (NO);
  81. }
  82.  
  83. /* score    - tell blackjack value of hand
  84.  */
  85. short score(h)
  86.    short h;       /* which hand */
  87. {
  88.    short val();
  89.  
  90.    short aces = 0;   /* number of aces in hand */
  91.    short i;          /* card counter */
  92.    short sum = 0;    /* accumulated value of hand */
  93.  
  94.    for (i = 0; i < ncards[h]; ++i) {
  95.       sum += val(h, i);
  96.       if (val(h, i) == 11)
  97.          ++aces;
  98.    }
  99.    for (i = aces; 0 < i; --i)
  100.       if (21 < sum)
  101.          sum -= 10;
  102.    return(sum);
  103. }
  104.  
  105. /* show     - print a card
  106.  */
  107. void show(h, i)
  108.    short h;       /* which hand */
  109.    short i;       /* which card */
  110. {
  111.    printf("%s", spots[hands[h][i] % 13]);
  112.    printf("%s", suits[hands[h][i] / 13]);
  113. }
  114.  
  115. /* split    - split the player's pair if allowed
  116.  */
  117. short split()
  118. {
  119.    void  show();
  120.    short tkcard(), val();
  121.  
  122.    if (val(1, 0) != val(1, 1))
  123.       return(1);
  124.    hands[2][0] = hands[1][1];
  125.    hands[1][1] = tkcard();
  126.    hands[2][1] = tkcard();
  127.    ncards[2] = 2;
  128.    printf("Hand 1: "); show(1, 0); printf(" + "); show(1, 1);
  129.    printf("\n");
  130.    printf("Hand 2: "); show(2, 0); printf(" + "); show(2, 1);
  131.    printf("\n");
  132.    tophand = 2;
  133.    return(2);
  134. }
  135.  
  136. /* val      - tell value of card n of hand h
  137.  */
  138. short val(h, i)
  139.    short h;    /* which hand */
  140.    short i;    /* which card */
  141. {
  142.    short n;    /* spots value of card */
  143.  
  144.    n = (hands[h][i] % 13) + 1;
  145.    if (n > 9)
  146.       return (10);
  147.    else if (n == 1)
  148.       return (11);
  149.    else
  150.       return (n);
  151. }
  152.  
  153.